import { defineStore } from 'pinia'

export const tokenStore = defineStore('tokenId', {
    state: () => ({
        token: ''
    }),
    actions: {
        saveToken(authToken) {
            const {accessToken, refreshToken, tokenExp, refreshTokenExp} = authToken
            this.token = accessToken
            localStorage.setItem("accessToken", accessToken)
            localStorage.setItem("refreshToken", refreshToken)
            localStorage.setItem("tokenExp", tokenExp)
            localStorage.setItem("refreshTokenExp", refreshTokenExp)
        },
        getToken() {
            try {
                return this.token || window.localStorage.getItem("accessToken") || ''
            } catch (e) {
                window.localStorage.setItem("accessToken", "")
                throw e
            }
        },
        // 移除token
        removeToken() {
            localStorage.removeItem("accessToken")
            localStorage.removeItem("refreshToken")
        }
    }
})
